namespace PickGold.Charting
{
	using System;
	using System.Collections.Generic;
	using System.Globalization;
	using System.Security.Permissions;
	using System.Web;

	[SRDescription("DescriptionAttributeDataPointComparer_DataPointComparer")]
	public class DataPointComparer : IComparer<DataPoint>
	{
		private PointSortOrder _sortingOrder;
		private int _sortingValueIndex;

		private DataPointComparer()
		{
			this._sortingValueIndex = 1;
		}

		[AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
		public DataPointComparer(Series series, PointSortOrder sortOrder, string sortBy)
		{
			this._sortingValueIndex = 1;
			sortBy = sortBy.ToUpper(CultureInfo.InvariantCulture);
			if (string.Compare(sortBy, "X", StringComparison.Ordinal) == 0)
			{
				this._sortingValueIndex = -1;
			}
			else if (string.Compare(sortBy, "Y", StringComparison.Ordinal) == 0)
			{
				this._sortingValueIndex = 0;
			}
			else if (string.Compare(sortBy, "AXISLABEL", StringComparison.Ordinal) == 0)
			{
				this._sortingValueIndex = -2;
			}
			else
			{
				if (((sortBy.Length != 2) || !sortBy.StartsWith("Y", StringComparison.Ordinal)) || !char.IsDigit(sortBy[1]))
				{
					throw new ArgumentException(SR.ExceptionDataPointConverterInvalidSorting, "sortBy");
				}
				this._sortingValueIndex = int.Parse(sortBy.Substring(1), CultureInfo.InvariantCulture) - 1;
			}
			if ((this._sortingValueIndex > 0) && (this._sortingValueIndex >= series.YValuesPerPoint))
			{
				throw new ArgumentException(SR.ExceptionDataPointConverterUnavailableSorting(sortBy, series.YValuesPerPoint.ToString(CultureInfo.InvariantCulture)), "sortBy");
			}
			this._sortingOrder = sortOrder;
		}

		public int Compare(DataPoint x, DataPoint y)
		{
			int num = -1;
			if (this._sortingValueIndex == -1)
			{
				num = x.XValue.CompareTo(y.XValue);
			}
			else if (this._sortingValueIndex == -2)
			{
				num = string.Compare(x.AxisLabel, y.AxisLabel, StringComparison.CurrentCulture);
			}
			else
			{
				num = x.YValues[this._sortingValueIndex].CompareTo(y.YValues[this._sortingValueIndex]);
			}
			if (this._sortingOrder == PointSortOrder.Descending)
			{
				num = -num;
			}
			return num;
		}
	}
}

